<?php
require_once 'db/db.php';
require_once 'util/perm.php';

function addUser( $userName, $pass, $email ) {
  try {
    $db = openDbConn();

    // validate for spaces and stuff like that
    $user = $db->quote($userName);
    $pass = $db->quote($pass);
    $email = $db->quote($email);
    $time = $db->quote(time());

    $db->beginTransaction();
    $db->exec("INSERT INTO user (name, pass, mail, permGroup, joinDate) 
                  VALUES ($user, $pass, $email, (SELECT id FROM permGroup WHERE name = 'user'), ${time} );");
    $db->commit();

  } catch ( PDOException $e ) {
    $db->rollback();
    print "ERROR: " . $e->getMessage() . "<br />";
    die();
  }
}

// string -> bool
function setPerm( $name, $perm, $pgGroupId = 0 ) {
  if( !hasPerm('edit.perm') ) 
    return -1;

  try {
    $db = openDbConn();   

    $name = $db->quote( $name );
    $perm = $db->quote( $perm );

// has to be ravamped
    if( $pgGroupId > 0 ) 
      $db->exec("UPDATE permGroup SET name = $pgGroupId, perm = $perm WHERE id = ${pgGroupId})");
    else
      $db->exec("INSERT INTO permGroup (name, perm) VALUES ($name, $perm);");
 
  } catch ( PDOException $e ) {
    print "ERROR: " . $e->getMessage() . "<br />";
    die();
  }
}

?>
